using System;
using System.IO;
using System.Reflection;

namespace Gonte.Reflection
{
    public static class EmbeddedResource
    {
        /// <summary>
        /// Return the names of the embedded resources in the assembly
        /// </summary>
        /// <param name="assembly">Assembly with the embedded resources if null, then it uses the assembly that is calling this method</param>
        /// <returns>The name of those resources</returns>
        public static string[] GetNames(Assembly assembly)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            return assembly.GetManifestResourceNames();
        }

        /// <summary>
        /// Return the information of the embedded resource in the assembly
        /// </summary>
        /// <param name="assembly">Assembly with the embedded resources if null, then it uses the assembly that is calling this method</param>
        /// <param name="resourceName">The name of the resource to get the info from</param>
        /// <returns>The information of the resource</returns>
        public static ManifestResourceInfo GetInfo(Assembly assembly, string resourceName)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            return assembly.GetManifestResourceInfo(resourceName);
        }
        
        /// <summary>
        /// Return the content of the embedded resource in the assembly
        /// </summary>
        /// <param name="assembly">Assembly with the embedded resources if null, then it uses the assembly that is calling this method</param>
        /// <param name="resourceName">The name of the resource to get the content from</param>
        /// <returns>The content of the resource</returns>
        public static Stream GetStream(Assembly assembly, string resourceName)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            Stream content = assembly.GetManifestResourceStream(resourceName);
            if (content == null)
            {
                throw new InvalidOperationException(string.Format("Unable to find embedded resource: {0}", resourceName));
            }

            return content;
        }

        /// <summary>
        /// Returns the embedded resource as a string object
        /// </summary>
        /// <param name="assembly">Assembly with the embedded resources if null, then it uses the assembly that is calling this method</param>
        /// <param name="resourceName">The name of the resource to get the content from</param>
        /// <returns>The content of the resource as a string</returns>
        public static string GetString(Assembly assembly, string resourceName)
        {
            using (StreamReader reader = new StreamReader(EmbeddedResource.GetStream(assembly, resourceName)))
            {
                return reader.ReadToEnd();
            }
        } 
    }
}
